<?php
class Warehouse_Service_OccupancyRate
{
    protected $_option = null; //array()
    protected $_shelf = null;
    protected $_product = null;
    protected $_error = null;
    protected $_rate = array(); //产品、仓库
    protected $_warehouseProduct = null;
    protected $_onHoldQuantity = 0; //未发货订单中配货数量
    protected $_onHold = false; //总库存是否存在冻结数量
    protected $_minValue = 1; //最小值
    /*
     *@param array('product_id'=>产品ID,'quantity'=>数量,'ws_cod'=>货架号);
     */
    public function __construct($params = null)
    {
        if (isset($params)) {
            foreach ($params as $key => $val) {
                $this->_option[$key] = $val;
            }
        }
        $this->_rate = array(
            'product' => 0,
            'shelf' => 0,
            'total' => 0,
        );
        $this->validator();
        $this->setData();
        if ($this->_error != '') {
            return array('ask' => 0, 'rate' => $this->_rate, 'msg' => $this->_error);
        }
        $this->calcRate();
        $this->calcShelfRate();
    }

    public function validator()
    {
        $error = array();
        $validateArr = array();
        if (isset($this->_option['product_id']) && $this->_option['product_id'] != '') {
            $validateArr[] = array('name' => '产品ID', 'value' => $this->_option['product_id'], 'regex' => array('positive1',));
            $validateArr[] = array('name' => '产品数量', 'value' => $this->_option['quantity'], 'regex' => array('require', 'positive1',));
        }
        $validateArr[] = array('name' => '货架', 'value' => $this->_option['ws_code'], 'regex' => array('require',));
        $error = Common_Service_Validator::formValidator($validateArr);
        if (!empty($error)) {
            $this->_error = join(',', $error);
            return false;
        }
    }

    /*
    * 设置产品、货架 资料
    */
    private function setData()
    {
        $error = array();
        if (isset($this->_option['product_id']) && $this->_option['product_id'] != '') {
            $productSer = new Product_Service_Product();
            $product = $productSer->getByProductId($this->_option['product_id']);
            if (empty($product)) {
                $error[] = '产品ID:' . $this->_option['product_id'] . '不存在!';
            } else {
                $this->_product = $product;
                if (isset($this->_product['product_length'])) {
                    $this->_product['product_length'] = $this->_product['product_length'] < $this->_minValue ? $this->_minValue : $this->_product['product_length'];
                    $this->_product['product_width'] = $this->_product['product_width'] < $this->_minValue ? $this->_minValue : $this->_product['product_width'];
                    $this->_product['product_height'] = $this->_product['product_height'] < $this->_minValue ? $this->_minValue : $this->_product['product_height'];
                }
            }

        }
        $shelfModel = new Warehouse_Model_Shelf();
        $shelf = $shelfModel->getByJoinClass($this->_option['ws_code']);
        if (empty($shelf)) {
            $error[] = '货架:' . $this->_option['ws_code'] . '不存在!';
        } else {
            $this->_shelf = $shelf;
        }
        if (!empty($error)) {
            $this->_error = join(',', $error);
            return false;
        }

        $warehouseProduct = Warehouse_Service_Products::getByCondition(array('warehouse_id' => 1, 'ws_code' => $this->_option['ws_code']));
        if (!empty($warehouseProduct)) {
            $this->_warehouseProduct = $warehouseProduct;
        }

        return true;
    }

    /*
    * 计算货架目前占用率
    */
    private function calcShelfRate()
    {
        if ($this->_warehouseProduct != '') {
            $productStocket = Product_Service_ProductStocket::getByProductId($this->_warehouseProduct['product_id'], 1);
            if (empty($productStocket)) {
                $this->_error = '产品ID:' . $this->_warehouseProduct['product_id'] . '总库存数据错误,无库存记录!';
                return false;
            } elseif ($productStocket['ps_quantity_hold'] > 0) {
                $this->_onHold = true;
            }

            $this->getByProcess();

            $productSer = new Product_Service_Product();
            $product = $productSer->getByProductId($this->_warehouseProduct['product_id']);
            $product['product_length'] = ($product['product_length'] < $this->_minValue) ? $this->_minValue : $product['product_length'];
            $product['product_width'] = ($product['product_width'] < $this->_minValue) ? $this->_minValue : $product['product_width'];
            $product['product_height'] = ($product['product_height'] < $this->_minValue) ? $this->_minValue : $product['product_height'];
            $productVolume = $product['product_length'] * $product['product_width'] * $product['product_height'];
            $shelfVolume = $this->_shelf['wsc_width'] * $this->_shelf['wsc_length'] * $this->_shelf['wsc_height'];
            $rate = $productVolume * ($this->_warehouseProduct['wp_quantity'] + $this->_warehouseProduct['wp_quantity_hold'] + $this->_onHoldQuantity) / $shelfVolume;
            $this->_rate['shelf'] = $rate * 100;
        } else {
            $this->_rate['shelf'] = 0;
        }

    }

    /*
    * 计算产品占用率
    */
    private function calcRate()
    {
        if ($this->_product != '') {
            $productVolume = $this->_product['product_length'] * $this->_product['product_width'] * $this->_product['product_height'];
            $shelfVolume = $this->_shelf['wsc_width'] * $this->_shelf['wsc_length'] * $this->_shelf['wsc_height'];
            $rate = ($productVolume * $this->_option['quantity']) / $shelfVolume;
            $this->_rate['product'] = $rate * 100;
        } else {
            $this->_rate['product'] = 0;
        }

    }

    /*
     * 判断货架是否还有其它未发货订单关联,返回数量
     */
    private function getByProcess()
    {
        if ($this->_onHold == false) {
            return true;
        }
        $qty = 0;
        $orderProduct = Order_Service_Product::getByCondition(array('product_id' => $this->_warehouseProduct['product_id']), 'orders_code', array('op_id desc'), 0, 0);
        if (!empty($orderProduct)) {
            foreach ($orderProduct as $key => $val) {
                $orders = Order_Service_Order::getByValue($val['orders_code'], 'orders_code', array('orders_code', 'orders_status', 'process_status'));
                if (isset($orders['orders_code']) && $orders['process_status'] < 5 && $orders['process_status'] > 2) {
                    $opmModel = new Order_Model_OrderPickupMap();
                    $orderPickupMap = $opmModel->getByfield('orders_code', $orders['orders_code']);
                    foreach ($orderPickupMap as $k => $v) {
                        $wpbRow = Warehouse_Service_ProductsBatch::getById($v['wpb_id']);
                        //如果有订单关联，返回配货数量
                        if ($wpbRow['warehouse_id'] == $this->_warehouseProduct['warehouse_id'] && $wpbRow['product_id'] == $this->_warehouseProduct['product_id'] && $wpbRow['ws_code'] == $this->_warehouseProduct['ws_code']) {
                            $qty += $v['opm_quantity'];
                        }
                    }
                }
            }
        }
        if ($qty > 0) {
            $this->_onHoldQuantity = $qty;
        }
    }

    /*
    * 获取占用率
    */
    public function getRate()
    {
        if ($this->_error != '') {
            return array('ask' => 0, 'rate' => $this->_rate, 'msg' => $this->_error);
        }
        $this->_rate['total'] = $this->_rate['product'] + $this->_rate['shelf'];
        foreach ($this->_rate as $key => $val) {
            $this->_rate[$key] = sprintf("%.2f", $val);
        }
        return array('ask' => 1, 'rate' => $this->_rate, 'msg' => 'Success');
    }

}